﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreFrame.Business;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Core.Orm;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.Dao.Owner
{
    public class OwnerManagerDao : AbstractDao
    {
        /// <summary>
        /// 是否存在该区域对象
        /// </summary>
        /// <param name="_areaCode"></param>
        /// <returns></returns>
        public bool ExistOwnerModel(string _ownerId)
        {
            string strWhere = "OwnerId = '" + _ownerId + "'";
            return BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Exists(strWhere);
        }

        [AOP(typeof(AopTransaction))]
        public int AddOwner(AbstractOwner ownerInfo)
        {
            BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Add(ownerInfo);
            string strSql = string.Format(@"select RoomId,RoomNumber from WY_Room where RoomNumber='{0}'", ownerInfo.RoomNumber);
            System.Data.DataTable dtroom = oleDb.GetDataTable(strSql);
            if (dtroom.Rows.Count>0)
           {
               strSql = string.Format(@"delete from WY_RoomOwnerRelation where RoomId='{0}'", dtroom.Rows[0]["RoomId"].ToString());
               oleDb.DoCommand(strSql);
               strSql = @"insert into WY_RoomOwnerRelation(OwnerId,RoomId,RoomNumber) values('" + ownerInfo.OwnerId + "','" + dtroom.Rows[0]["RoomId"].ToString() + "','" + ownerInfo.RoomNumber + "')";
               oleDb.DoCommand(strSql);
           }
            return 0;
        }

        /// <summary>
        /// 房间关联人员
        /// </summary>
        /// <param name="ownerInfo"></param>
        /// <param name="_roomId"></param>
        /// <returns></returns>
        public int AddOwner(AbstractOwner ownerInfo,string _roomId)
        {
            int ret = 0;
            string strSql = "";
            ret = BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Add(ownerInfo);
            if (ownerInfo.POwnerId == null || ownerInfo.POwnerId == "")//新增业主才更新业主房屋表
            {
                
                strSql = @"insert into WY_RoomOwnerRelation(OwnerId,RoomId,RoomNumber) values('" + ownerInfo.OwnerId + "','" + _roomId + "','"+ownerInfo.RoomNumber+"')";
                ret = oleDb.DoCommand(strSql);
            }
           
            return ret;
        }

       /// <summary>
       /// 门面关联人员
       /// </summary>
       /// <param name="ownerInfo"></param>
       /// <returns></returns>
        public int AddShopOwner(AbstractOwner ownerInfo,string _shopId)
        {
            int ret = 0;
            ret = BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Add(ownerInfo);
            if (ownerInfo.POwnerId == null)//新增业主才更新业主房屋表
            {
                string strwhere = "ShopId='" + _shopId + "'";
                string strsetid="OwnerId='" + ownerInfo.OwnerId + "'";
                string strsetName="OwnerName='" + ownerInfo.Name + "'";
                BindEntity<AbstractShop>.CreateInstanceDAL(oleDb, WorkId).Update(strwhere, strsetid, strsetName);
            }

            return ret;
        }


        public int UpdateOwner(AbstractOwner ownerInfo)
        {
            int ret = 0;
            ret = BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Update(ownerInfo);
            return ret;
        }

        public bool DeleteOwner(string _ownerId)
        {
            string strWhere = "OwnerId = '" + _ownerId + "'";
            return BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).Delete(strWhere) > 0;
        }

        public List<AbstractOwner> GetOwnerList(string _pOwnerId)
        {
            if (_pOwnerId == "")
            {
                return null;
            }
            string strWhere = "POwnerId = '" + _pOwnerId + "'";
            return BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strWhere);
        }

        public List<AbstractOwner> GetOwnerList(PageInfo page, string personName, string IdCard, string mobile)
        {
            string strWhere="";
            if (personName != "")
                strWhere = strWhere + "Name like '%" + personName + "%'";
            if (IdCard != "")
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + "  or  CardNum='%" + IdCard + "%'";
                }
                else
                {
                    strWhere = strWhere + "CardNum='%" + IdCard + "%'";
                }
            }
            if (mobile != "")
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + "  or  OftenMobile='%" + mobile + "%'";
                }
                else
                {
                    strWhere = strWhere + "OftenMobile='%" + mobile + "%'";
                }
            }
            if (strWhere == "")
            {
                strWhere = "1=1";
            }

            string strSearch = " OwnerId in ( " +
                      "     SELECT  " +
                      "    (case when pownerid is NULL then OwnerId else pownerid end) OwnerId " +
                      "      FROM WY_Owner " +
                      "    where   " +strWhere+""+ 
                      "    ) or pownerid in ( " +
                      "    SELECT  " +
                      "    (case when pownerid is NULL then OwnerId else pownerid end) OwnerId " +
                      "      FROM WY_Owner " +
                      "    where  " + strWhere + "" + 
                      "    )";

            return BindEntity<AbstractOwner>.CreateInstanceDAL(oleDb, WorkId).GetListArray(strSearch, page);

        }
    }
}
